package com.whxd.framework.common.json;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * 自定义JSON序列化的时候日期时间格式
 * 
 * @author 张世鹏
 * @since 2013-6-15
 */
public class CustomDateTimeSerializer extends JsonSerializer<Date>{

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 自定义JSON序列化的时候日期时间格式:yyyy-MM-dd hh:mm:ss
	 * 
	 * @author 张世鹏
	 * @since 2013-6-15
	 * @param paramT
	 * @param paramJsonGenerator
	 * @param paramSerializerProvider
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @see org.codehaus.jackson.map.JsonSerializer#serialize(java.lang.Object,
	 *      org.codehaus.jackson.JsonGenerator,
	 *      org.codehaus.jackson.map.SerializerProvider)
	 */
	@Override
	public void serialize(Date date,JsonGenerator gen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException{
		String formattedDate = dateFormat.format(date);
		gen.writeString(formattedDate);
	}
}
